﻿using MySQLDriverCS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;

namespace SafeDoorService
{
    /// <summary>
    /// 图书操作Service
    /// </summary>
    public interface BookService
    {
        /// <summary>
        /// 根据RFID查询图书信息
        /// </summary>
        /// <param name="Rfid">图书的RFID号</param>
        /// <returns></returns>
        CBookInfo getBook(String Rfid);

        /// <summary>
        /// 根据rfid查询图书是否正在借阅中
        /// </summary>
        /// <param name="rfid">RFID号</param>
        /// <returns>true 借阅中 false 其他</returns>
        Boolean isBorrowed(String rfid);
    }

    /// <summary>
    /// 图书操作实现类
    /// </summary>
    public class BookServiceImpl : BookService
    {
        private string lastError;

        /// <summary>
        /// 错误信息
        /// </summary>
        public string LastError
        {
            get { return lastError; }
        }
        /// <summary>
        /// 连接字符串
        /// </summary>
        private static readonly string LocalDbName = "LocalDB"; 

        public CBookInfo getBook(string Rfid)
        {
            CBookInfo book = new CBookInfo();
            MySQLConnection conn = new MySQLConnection(clsCommon.GetConnStr(ConfigurationManager.AppSettings[LocalDbName]));
            try
            {
                conn.Open();
            }
            catch (MySQLException ex)
            {
                lastError = "打开网络失败:" + ex.Message;
                return book;
            }
            // 处理中文乱码
            MySQLCommand cmd = new MySQLCommand("set names gb2312", conn);
            try
            {
                cmd.ExecuteNonQuery();
                string sql = "SELECT RFID,barcode,bookName,isBorrowed from bookinfo where rfid ='" + Rfid + "' LIMIT 0,1"; 
                MySQLDataAdapter mda = new MySQLDataAdapter(sql, conn);
                DataSet ds = new DataSet();
                mda.Fill(ds, "bookinfo");
                if (ds != null && ds.Tables.Count > 0)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count > 0)
                    {
                        DataRow row = dt.Rows[0];
                        book.BookTitle = Convert.ToString(row["bookName"]);
                        book.RFID = Convert.ToString(row["RFID"]);
                        book.Barcode = Convert.ToString(row["barcode"]);
                        book.IsBorrow = Convert.ToInt32(row["isBorrowed"]);
                    } 
                }

                return book;
            }
            catch (MySQLException ex)
            {
                lastError = "查询出现异常：" + ex.Message;
                return book;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
        }


        public bool isBorrowed(string rfid)
        { 
            MySQLConnection conn = new MySQLConnection(clsCommon.GetConnStr(ConfigurationManager.AppSettings[LocalDbName]));
            try
            {
                conn.Open();
            }
            catch (MySQLException ex)
            {
                lastError = "打开网络失败:" + ex.Message;
                return false;
            }
            // 处理中文乱码
            MySQLCommand cmd = new MySQLCommand("set names gb2312", conn);
            try
            {
                cmd.ExecuteNonQuery();
                string sql = "SELECT cbid from currentborrowrecord where rfid ='" + rfid + "'";
                cmd = new MySQLCommand(sql,conn);
                Object obj = cmd.ExecuteScalar();
                if(obj != null)
                    return true; 
                return false;
            }
            catch (MySQLException ex)
            {
                lastError = "查询出现异常：" + ex.Message;
                return false;
            }
            finally
            {
                cmd.Dispose();
                conn.Close();
            }
        }
    } 
}
